#include <basic_types.h>
#include <mediatypes.h>	
#include <api/libc/alloc.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <hld/hld_dev.h>
#include <hld/vbi/vbi.h>
#include <hld/vbi/vbi_dev.h>

#ifdef DUAL_ENABLE
#include <modules.h>

enum HLD_VBI_FUNC{
    FUNC_VBI_OPEN = 0,
    FUNC_VBI_CLOSE,
    FUNC_VBI_IO_CONTROL,
    FUNC_VBI_SET_OUTPUT,
    FUNC_VBI_START,
    FUNC_VBI_STOP,
    FUNC_VBI_DEFAULT_GO_SET,
};

#ifndef _HLD_VBI_REMOTE
UINT32 hld_vbi_entry[] = 
{
    (UINT32)vbi_open,
    (UINT32)vbi_close,
    (UINT32)vbi_ioctl,
    (UINT32)vbi_setoutput,
    (UINT32)vbi_start,
    (UINT32)vbi_stop,
    (UINT32)ttx_default_g0_set,
};

void hld_vbi_callee(UINT8 *msg)
{
    OS_hld_callee(hld_vbi_entry, msg);
}
#endif

#ifdef _HLD_VBI_REMOTE

UINT32 desc_vbi_p_uint32[] = 
{ //desc of pointer para
  1, DESC_OUTPUT_STRU(0, 4),  
  1, DESC_P_PARA(0, 1, 0),
  //desc of pointer ret
  0,                          
  0,
};

UINT32 desc_vbi_io_ctrl_p_uint32[] = 
{ //desc of pointer para
  1, DESC_OUTPUT_STRU(0, 4),  
  1, DESC_P_PARA(0, 2, 0),
  //desc of pointer ret
  0,                          
  0,
};

UINT32 desc_vbi_io_ctrl_ttx_page_info[] = 
{ //desc of pointer para
  1, DESC_STATIC_STRU(0, sizeof(struct ttx_page_info)),
  1, DESC_P_PARA(0, 2, 0), 
  //desc of pointer ret
  0,                          
  0,
};

/*
 * 	Name		:   vbi_open()   	
 *	Description	:   Open a vbi device
 *	Parameter	:	struct vbi_device *dev		: Device to be openned
 *	Return		:	INT32 						: Return value
 *
 */
INT32 vbi_open(struct vbi_device *dev)
{
	jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(1<<16)|FUNC_VBI_OPEN, NULL);
}

/*
 * 	Name		:   vbi_close()   	
 *	Description	:   Close a vbi device
 *	Parameter	:	struct vbi_device *dev		: Device to be closed
 *	Return		:	INT32 						: Return value
 *
 */
INT32 vbi_close(struct vbi_device *dev)
{
    jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(1<<16)|FUNC_VBI_CLOSE, NULL);
}


/*
 * 	Name		:   vbi_io_control()   	
 *	Description	:   vbiel IO control command
 *	Parameter	:	struct sto_device *dev		: Device
 *					INT32 cmd					: IO command
 *					UINT32 param				: Param
 *	Return		:	INT32 						: Result
 *
 */
INT32 vbi_ioctl(struct vbi_device *dev, UINT32 cmd, UINT32 param)
{
    UINT32 *desc = NULL;
    
	switch(cmd)
    {
    case IO_VBI_WORK_MODE_SEPERATE_TTX_SUBT:
    case IO_VBI_WORK_MODE_HISTORY :
    case IO_VBI_ENGINE_OPEN:
    case IO_VBI_ENGINE_CLOSE:
    case IO_VBI_ENGINE_UPDATE_PAGE:
    case IO_VBI_ENGINE_SHOW_ON_OFF:
    case IO_VBI_ENGINE_SEND_KEY:
    case IO_VBI_ENGINE_SET_CUR_LANGUAGE:
    case IO_VBI_SELECT_OUTPUT_DEVICE:
    default:
        desc = NULL;
        break;
    case IO_VBI_ENGINE_GET_STATE:
        desc = desc_vbi_io_ctrl_p_uint32;
        break;
    case IO_VBI_ENGINE_UPDATE_INIT_PAGE:
    case IO_VBI_ENGINE_UPDATE_SUBT_PAGE:
        desc = desc_vbi_io_ctrl_ttx_page_info;
        break;
    }

    jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(3<<16)|FUNC_VBI_IO_CONTROL, desc);
	
}

void vbi_setoutput(struct vbi_device *dev,T_VBIRequest *pVBIRequest)
{
	jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(2<<16)|FUNC_VBI_SET_OUTPUT, desc_vbi_p_uint32);
}

INT32 vbi_start(struct vbi_device *dev,t_TTXDecCBFunc pCBFunc)
{
    jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(2<<16)|FUNC_VBI_START, NULL);
}

INT32 vbi_stop(struct vbi_device *dev)
{
    jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(1<<16)|FUNC_VBI_STOP, NULL);
}

void ttx_default_g0_set(struct vbi_device *dev, UINT8 default_g0_set)
{
	jump_to_func(NULL, OS_hld_caller, dev, (HLD_VBI_MODULE<<24)|(2<<16)|FUNC_VBI_DEFAULT_GO_SET, NULL);
}

//For S3602F, below 2 functions shouldnt be called from CPU, leaving them is just for link issue when link to old DMX
INT32 vbi_request_write(void * pdev, UINT32 uSizeRequested,void ** ppuData,UINT32* puSizeGot,struct control_block* pDataCtrlBlk)
{
    ASSERT(0);
}

void vbi_update_write(void * pdev, UINT32 uDataSize)
{
    ASSERT(0);
}

#endif

#endif

